package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.entity.activity.lottery.UserActivityLotteryData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryConfig;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lottery.ActivityLotteryMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_LOTTERY_NOTICE_READ, desc = "抽奖公告已读")
public class ReadActivityLotteryNoticeCmd extends AbstractCommand {

    int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityLotteryProto.ActivityLotteryNoticeReadReqMsg reqMsg = ActivityLotteryProto.ActivityLotteryNoticeReadReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        ActivityLotteryConfig config = ActivityLotteryMgr.getActivityLotteryConfig(activityId);
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.Builder builder = ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.newBuilder();
        UserActivityLotteryData userData = player.getModule(ActivityLotteryModule.class).getUserData(activityId);
        if (null == userData){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        ret = 0;
        userData.setNoticeReadTime(reqMsg.getReadTime());
        player.getModule(ActivityLotteryModule.class).syncUserData();

        ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.Builder resp = ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setActivityId(activityId);
        builder.setReadTime(reqMsg.getReadTime());
        player.sendPacket(Protocol.U_ACTIVITY_LOTTERY_NOTICE_READ, resp);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.Builder builder = ActivityLotteryProto.ActivityLotteryNoticeReadRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setActivityId(activityId);
            player.sendPacket(Protocol.U_ACTIVITY_LOTTERY_NOTICE_READ, builder);
        }
    }
}
